/*
 * @Date: 2019-11-25 18:00:01
 * @Author: 情雨随风
 * @LastEditors: 情雨随风
 * @LastEditTime: 2019-11-26 15:33:49
 * @Description: 处理微信环境
 */

import Vue from 'vue';
import { AuthOpenid } from '@/api/common';

//判断是否是微信环境
export const isWeChat = () => (/micromessenger/i).test(navigator.userAgent)


//微信环境处理
export const WeChat = async (route) => {
    const openid = Vue.ls.get('openid');
    if(openid){
        //已经存在openid  不处理
        console.log(openid)
        return;
    }
    
    const code = route.code
    if(code) {
        //存在code
        const [err, res] = await AuthOpenid({ id: code })

        console.log(res)
        if(!err && res.ResultCode == 1) {
            Vue.ls.set('openid', res.Data)
        }
    }
    else {
        //不1存在code  需授权获取code
        let url = "https://open.weixin.qq.com/connect/oauth2/authorize?"
                + "appid=" + "wx2fc3682c73719247"
                // + "&redirect_uri=" + encodeURIComponent('http://ceshi.jiandaopay.com/sytem')
                + "&redirect_uri=" + encodeURIComponent(window.location.href)
                + "&response_type=code"
                + "&scope=snsapi_base"
                + "#wechat_redirect"
        window.location.replace(url)
        console.log(window.location.href)
    }
}

